package com.dance.java8.day01.date;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

public class TestLocalDateTime {

    /**
     * 1: LocalDate LocalTime LocalDateTime
     */
    @Test
    public void test1(){
        // 获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        // 打印
        System.out.println("当前时间为:"+localDateTime);

        // 指定日期
        LocalDateTime of = LocalDateTime.of(2021, 12, 4, 17, 49, 12);
        System.out.println("指定日期和时间:"+of);

        // 日期时间计算
        // + 1 day
        System.out.println("添加一天:" + localDateTime.plusDays(1));
        // -1 day
        System.out.println("减去一天:" + localDateTime.plusDays(-1));
        // -3 year
        System.out.println("减去三年:" + localDateTime.minusYears(3));
        // 获取
        System.out.println(localDateTime.getYear());
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getHour());
        System.out.println(localDateTime.getMinute());
        System.out.println(localDateTime.getSecond());
    }

    /**
     * 2:Instant : 时间戳(以Unix 元年: 1970年1月1日 00:00:00 到某个时间之间的毫秒值)
     */
    @Test
    public void test2(){
        Instant now = Instant.now();
        System.out.println("时间为:"+now); // 默认获取UTC 时区 格林尼治时间
        // 小时偏移量计算
        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println("偏移后的时间:"+offsetDateTime);
        // 转换为毫秒
        System.out.println("毫秒:"+now.toEpochMilli());
        System.out.println("系统:"+System.currentTimeMillis());
        // 偏移开始时间
        Instant instant = Instant.ofEpochSecond(60);
        System.out.println("元年偏移时间:"+instant);
    }

    /**
     * 3:
     *  Duration : 计算两个时间之间的间隔
     *  Period : 计算两个日期之间的间隔
     */
    @Test
    public void test3() throws InterruptedException {
        // 时间间隔
        Instant start = Instant.now();
        Thread.sleep(1000);
        Instant end = Instant.now();
        Duration between = Duration.between(start, end);
        System.out.println("时间间隔为:"+between.getSeconds());

        LocalTime startTime = LocalTime.now();
        Thread.sleep(1000);
        LocalTime endTime = LocalTime.now();
        long seconds = Duration.between(startTime, endTime).getSeconds();
        System.out.println("时间间隔为:" + seconds);
    }

    @Test
    public void test4(){
        LocalDate now = LocalDate.now();
        LocalDate localDate = LocalDate.of(2022,3,4);
        Period between = Period.between(now, localDate);
        System.out.println("日期间隔:"+between);
    }

    @Test
    public void test5(){
        LocalDateTime now = LocalDateTime.now();
        System.out.println("当前时间为:"+now);
        // 指定日期为几号
        LocalDateTime localDateTime = now.withDayOfMonth(10);
        System.out.println("with date after:"+localDateTime);
        // 时间矫正器
        // 调整到下周日
        LocalDateTime with = now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        System.out.println("下周日为:"+with);

        // 自定义下一个工作日
        LocalDateTime workDay = now.with(l -> {
            LocalDateTime work = (LocalDateTime) l;
            DayOfWeek dayOfWeek = work.getDayOfWeek();
            switch (dayOfWeek) {
                case FRIDAY:
                    return work.plusDays(3);
                case SATURDAY:
                    return work.plusDays(2);
                default:
                    return work.plusDays(1);
            }
        });
        System.out.println("下一个工作日:"+workDay);
    }

    /**
     * DateTimeFormatter : 格式化时间 / 日期
     */
    @Test
    public void test6(){
        // 使用提供的格式化类
        DateTimeFormatter isoDate = DateTimeFormatter.ISO_DATE;
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(isoDate);
        System.out.println(format);

        // 自定义格式化
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format1 = now.format(dateTimeFormatter);
        System.out.println(format1);
    }

    /**
     * ZonedDate ZonedTime ZonedDateTime
     */
    @Test
    public void test7(){
        // 打印所有时区
//        ZoneId.getAvailableZoneIds().forEach(System.out::println);
        // 指定时区获取时间
        LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        System.out.println(now);

        // 获取携带时区
        ZonedDateTime zonedDateTime = now.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zonedDateTime);
    }
}
